<?php

namespace VeCongTy\Model;

use Zend\Db\TableGateway\TableGateway;

class TabsTable {
    
    protected $tableGateway;

    public function getTableGateway() {
        return $this->tableGateway;
    }

    public function setTableGateway($tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }
    
    public function addTab($data)
    {
        $key_name = $data['key_name'];
        
        $name = $data['name'];
        
        if(isset($data['value']))
        {
            $value = $data['value'];
        }
        else {
            $value = null;
        }
        
        if(isset($data['weight']))
        {
            $weight = $data['weight'];
        }
        else
        {
            $weight = null;
        }
        
        $this->tableGateway->insert(array(
            'key_name'  => $key_name,
            'name'      => $name,
            'value'     => $value,
            'weight'    => $weight
        ));
    }
    
    public function isExist($key_name, $name)
    {
        $where = new \Zend\Db\Sql\Where();
        
        $where->equalTo('key_name', $key_name)->OR->equalTo('name', $name);
        
        $result = $this->tableGateway->select($where);
        if($result->count())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public function getAll()
    {
        $resultSet = $this->tableGateway->select();
        return $resultSet->toArray();
    }
    
    public function save($value, $key_name)
    {
        $this->tableGateway->update(array('value' => $value), array('key_name' => $key_name));
    }
    
    public function deleteTab($key_name)
    {
        $this->tableGateway->delete(array('key_name' => $key_name));
    }
}